﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;

public partial class codelist : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    //删除

    protected void gv_record_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

        string sqlstr = "delete from t_record where irecord_ID='" + gv_record.DataKeys[e.RowIndex].Value.ToString() + "'";

        string str = ConfigurationManager.ConnectionStrings["DevMgmntConnectionString"].ConnectionString.ToString();
        SqlConnection cn33 = new SqlConnection(str);
        cn33.Open();
        SqlCommand sqlcom = new SqlCommand(sqlstr, cn33);

        sqlcom.ExecuteNonQuery();

        cn33.Close();

        gv_record.DataBind();

    }


    protected void bt_recordAdd_Click(object sender, EventArgs e)
    {


        string fullFileName = this.UPLOADfile.PostedFile.FileName;
        //从路径中截取出文件名
        string fileName = fullFileName.Substring(fullFileName.LastIndexOf("\\") + 1);
        //限定上传文件的格式
        string type = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1);

        string ipath = Server.MapPath("record"); //创建文件夹时用
        int id = (int)gv_record.DataKeys[gv_record.Rows.Count - 1].Value + 1;
        string SAipath = ipath + "\\" + id.ToString() + ".rar"; //上传到服务器上后的路径(实际路径),"\\"必须为两个斜杠,在C#中一个斜杠表示转义符.

        if (!System.IO.Directory.Exists(ipath)) //判断文件夹是否已经存在
            System.IO.Directory.CreateDirectory(ipath); //创建文件夹
        
        if (System.IO.File.Exists(SAipath))
            System.IO.File.Delete(SAipath);


        
        

        try
        {
            string str = ConfigurationManager.ConnectionStrings["DevMgmntConnectionString"].ConnectionString.ToString();
            SqlConnection cn33 = new SqlConnection(str);
            cn33.Open();
            str = "insert INTO t_record ([sName], [dBirth], [iSex], [iCulture_ID], [sHealthy], [sPolitics], [iProfessiona_ID], [iDepartment_ID], [sRemark]) values ('"
                + tx_name.Text.Trim() + "','" + tx_birth.Text.Trim() + "','" + sel_sex.SelectedValue + "','" + sel_culture.SelectedValue + "','" 
                + sel_healthy.SelectedValue + "','" + tx_politics.Text.Trim() + "','" + sel_professiona.SelectedValue + "','" + sel_department.SelectedValue + "','" + tx_master.Text.Trim() + "')";
            Console.WriteLine("btn_recordAdd:{0}", str);
            SqlCommand cmd2 = new SqlCommand(str, cn33);
            cmd2.ExecuteNonQuery();
            cn33.Close();

            

        }
        catch (Exception ex)
        {
            Console.WriteLine("传递过来的异常值为：{0}", ex);
            TX_UPLOAD.Text = "添加失败！";
            return;
        }

        if (type == "rar")//根据后缀名来限制上传类型
        {

            UPLOADfile.PostedFile.SaveAs(SAipath);
            //TX_UPLOAD.Text = "上传成功！";
        }
        else if (fileName!=null && fileName!="")
        {
            TX_UPLOAD.Text = "上传失败,仅支持(.rar)格式的文件上传!";

        }

        gv_record.DataBind();
    }


    
    protected void gv_RowCreated(object sender, GridViewRowEventArgs  e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton lb = e.Row.FindControl("lbtn_down") as LinkButton;

            int id = (int)gv_record.DataKeys[e.Row.RowIndex].Value;
            
            string file = Server.MapPath("record/" + id.ToString() + ".rar");
            if (System.IO.File.Exists(file) == false)
            {
                lb.Enabled = false;
            }  
          
        }
    }

  

    protected void btn_down(object sender, EventArgs e)
    {
        LinkButton lb = sender as LinkButton;
        GridViewRow gvr = (GridViewRow)lb.Parent.Parent;
        int id = (int)gv_record.DataKeys[gvr.RowIndex].Value;
        string file = id.ToString() + ".rar";
        string pfile = Server.MapPath("record/" + file);


        FileInfo fileInfo = new FileInfo(pfile);
        Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();
        Response.AddHeader("Content-Disposition", "attachment;filename=" + file);
        Response.AddHeader("Content-Length", fileInfo.Length.ToString());
        Response.AddHeader("Content-Transfer-Encoding", "binary");
        Response.ContentType = "application/octet-stream";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
        Response.WriteFile(fileInfo.FullName);
        Response.Flush();
        Response.End();

    }


    protected void Button1_Click(object sender, EventArgs e)
    {
   
        return;



    }
    
}


